#include "utils/ring_buffer.h"
#include <gtest/gtest.h>
#include <thread>
#include <vector>
#include <chrono>

using namespace hft::utils;

// 基本功能测试
TEST(RingBufferTest, BasicOperations) {
    RingBuffer<int, 4> buffer;
    
    // 初始状态检查
    EXPECT_TRUE(buffer.empty());
    EXPECT_FALSE(buffer.full());
    EXPECT_EQ(buffer.size(), 0);
    EXPECT_EQ(buffer.capacity(), 4);

    // 写入测试
    EXPECT_TRUE(buffer.try_push(1));
    EXPECT_TRUE(buffer.try_push(2));
    EXPECT_FALSE(buffer.empty());
    EXPECT_EQ(buffer.size(), 2);

    // 读取测试
    int value;
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 1);
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 2);
    EXPECT_TRUE(buffer.empty());
}

// 边界条件测试
TEST(RingBufferTest, BoundaryConditions) {
    RingBuffer<int, 4> buffer;

    // 填满缓冲区
    EXPECT_TRUE(buffer.try_push(1));
    EXPECT_TRUE(buffer.try_push(2));
    EXPECT_TRUE(buffer.try_push(3));
    EXPECT_TRUE(buffer.try_push(4));
    EXPECT_TRUE(buffer.full());
    EXPECT_FALSE(buffer.try_push(5));  // 缓冲区已满，写入应该失败

    // 清空缓冲区
    int value;
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 1);
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 2);
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 3);
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 4);
    EXPECT_FALSE(buffer.try_pop(value));  // 缓冲区为空，读取应该失败
}

// 环形特性测试
TEST(RingBufferTest, CircularBehavior) {
    RingBuffer<int, 4> buffer;

    // 填充-清空-填充循环
    for (int cycle = 0; cycle < 3; ++cycle) {
        EXPECT_TRUE(buffer.try_push(1));
        EXPECT_TRUE(buffer.try_push(2));
        EXPECT_TRUE(buffer.try_push(3));
        
        int value;
        EXPECT_TRUE(buffer.try_pop(value));
        EXPECT_EQ(value, 1);
        EXPECT_TRUE(buffer.try_pop(value));
        EXPECT_EQ(value, 2);
        EXPECT_TRUE(buffer.try_pop(value));
        EXPECT_EQ(value, 3);
    }
}

// 并发安全性测试
TEST(RingBufferTest, ThreadSafety) {
    RingBuffer<int, 1024> buffer;
    constexpr int num_items = 10000;
    
    // 生产者线程
    std::thread producer([&]() {
        for (int i = 0; i < num_items; ++i) {
            while (!buffer.try_push(i)) {
                std::this_thread::yield();
            }
        }
    });

    // 消费者线程
    std::vector<int> received;
    received.reserve(num_items);
    std::thread consumer([&]() {
        int value;
        while (received.size() < num_items) {
            if (buffer.try_pop(value)) {
                received.push_back(value);
            } else {
                std::this_thread::yield();
            }
        }
    });

    producer.join();
    consumer.join();

    // 验证所有数据都被正确接收
    EXPECT_EQ(received.size(), num_items);
    for (int i = 0; i < num_items; ++i) {
        EXPECT_EQ(received[i], i);
    }
}

// 性能基准测试
TEST(RingBufferTest, PerformanceBenchmark) {
    RingBuffer<int, 1024> buffer;
    constexpr int num_operations = 1000000;

    // 测量单线程写入性能
    auto start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < num_operations; ++i) {
        while (!buffer.try_push(i)) {
            std::this_thread::yield();
        }
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    
    // 输出性能统计
    double ops_per_second = static_cast<double>(num_operations) / 
                           (duration.count() / 1000000.0);
    std::cout << "Performance: " << ops_per_second << " operations/second" << std::endl;
    
    // 清空缓冲区
    int value;
    while (buffer.try_pop(value)) {}
}

// 清空操作测试
TEST(RingBufferTest, ClearOperation) {
    RingBuffer<int, 4> buffer;

    // 填充数据
    EXPECT_TRUE(buffer.try_push(1));
    EXPECT_TRUE(buffer.try_push(2));
    EXPECT_EQ(buffer.size(), 2);

    // 清空缓冲区
    buffer.clear();
    EXPECT_TRUE(buffer.empty());
    EXPECT_EQ(buffer.size(), 0);

    // 确认可以继续使用
    EXPECT_TRUE(buffer.try_push(3));
    int value;
    EXPECT_TRUE(buffer.try_pop(value));
    EXPECT_EQ(value, 3);
}